package edu.nf.supermarket.web.controller.app;

import edu.nf.supermarket.base.BaseController;
import edu.nf.supermarket.entity.ProductSpec;
import edu.nf.supermarket.result.ResultVO;
import edu.nf.supermarket.service.ProductSpecService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author gwf
 * @date 2025-06-18
 */
@RestController
@RequestMapping("/productSpec")
public class ProductSpecController extends BaseController {

    @Autowired
    private ProductSpecService productSpecService;

    /**
     * 获取商品规格信息
     * @param productId
     * @return
     */
    @GetMapping("/getProductSpec")
    public ResultVO<List<ProductSpec>> getProductSpec(@RequestParam("productId") Integer productId){
        try {
            List<ProductSpec> productSpecs = productSpecService.getProductSpecsByProductId(productId);
            return success(productSpecs);
        }catch (Exception e){
            return fail(500, "获取商品规格信息失败");
        }
    }

    /**
     * 获取所有商品规格信息
     * @return
     */
    @GetMapping("/getAllSpecs")
    public ResultVO<List<ProductSpec>> getAllSpecs(){
        try {
            List<ProductSpec> specs = productSpecService.getAllActiveSpecs();
            return success(specs);
        }catch (Exception e){
            return fail(500, "服务器异常");
        }
    }

    /**
     * 根据规格ID获取规格信息
     * @param specId
     * @return
     */
    @GetMapping("/getSpecById")
    public ResultVO<ProductSpec> getSpecById(@RequestParam("specId") Integer specId){
        try {
            ProductSpec productSpec = productSpecService.getSpecById(specId);
            if (productSpec != null){
                return success(productSpec);
            }else {
                return fail(404, "规格不存在");
            }
        }catch (Exception e){
            return fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), "服务器繁忙，请稍后...");
        }
    }
}
